﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CMS.Controls
{
    public class MyButton:Button
    {
        public MyButton()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
        }

        #region 控件绘制相关



        /// <summary>
        /// 文字的布局参数
        /// </summary>
        private StringFormat _StringFormat = new StringFormat() { Alignment=StringAlignment.Center,LineAlignment=StringAlignment.Center};

        /// <summary>
        /// 按钮前景色画刷
        /// </summary>
        private Brush _ForeBrush=Brushes.Black;

        /// <summary>
        /// 按钮背景色画刷
        /// </summary>
        private Brush _BackBrush = Brushes.White;

        /// <summary>
        /// 鼠标经过时的附加画刷
        /// </summary>
        private Brush _HoverBrush = new SolidBrush(Color.FromArgb(40, Color.SkyBlue));


        protected override void OnForeColorChanged(EventArgs e)
        {
            base.OnForeColorChanged(e);
            _ForeBrush = new SolidBrush(ForeColor);
        }

        protected override void OnBackColorChanged(EventArgs e)
        {
            base.OnBackColorChanged(e);
            _BackBrush = new SolidBrush(BackColor);
        }

        bool _IsHover = false;

        bool _IsMouseDown = false;

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseHover(e);
            _IsHover = true;
            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
           
            base.OnMouseLeave(e);
            _IsHover = false;
            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            _IsMouseDown = true;
            base.OnMouseDown(mevent);
          
            //Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            _IsMouseDown = false;
            base.OnMouseUp(mevent);
        }

        private readonly Brush _DisableBrush = Brushes.WhiteSmoke;

        protected override void OnPaint(PaintEventArgs pevent)
        {
            var g = pevent.Graphics;

            Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
            
            if (Enabled)
            {
                g.FillRectangle(_BackBrush, rect);
                if (_IsMouseDown)
                {
                    g.FillRectangle(_HoverBrush, rect);
                }
                if (_IsHover)
                {
                    g.FillRectangle(_HoverBrush, rect);
                }
            } else
            {
                g.FillRectangle(_DisableBrush, rect);
            }
            
            g.DrawRectangle(Pens.LightGray, rect);
            g.DrawString(Text, Font, _ForeBrush, new Rectangle(Point.Empty,Size), _StringFormat);
        }
        #endregion

    }
}
